package com.dw.etl.core.controller;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.common.util.LocalDateUtil;
import com.dw.etl.common.util.SpringBeanUtil;
import com.dw.etl.core.api.EtlBaseService;
import com.dw.etl.core.api.EtlIndexsqlService;
import com.dw.etl.core.api.EtlRunService;
import com.dw.etl.core.bean.model.EtlBase;
import com.dw.etl.core.bean.vo.DmSqlVO;
import com.dw.etl.core.bean.vo.JobQryVO;
import com.dw.etl.core.config.EtlConstant;
import com.dw.etl.core.utils.EtlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by liujunlei on 2021/5/21.
 */
@RestController
@RequestMapping("/etl/etlBase/")
@Api(tags="任务运行")
public class RunJobController {

    @Resource(name = "myJobLauncher")
    SimpleJobLauncher myJobLauncher;

    @Autowired
    EtlBaseService etlBaseService;

    @Autowired
    EtlRunService etlRunService;

    @ApiOperation(value="自动建表")
    @RequestMapping(value="autoCreateTable", method= RequestMethod.GET)
    public BaseResult autoCreateTable(String idBase) throws BaseException {
        etlRunService.autoCreatTable(idBase);
        return new BaseResult(CommonResultConstant.SUCCESS, "执行成功");
    }

    @ApiOperation(value="手工执行天任务")
    @RequestMapping(value="jobExecByHand", method= RequestMethod.GET)
    public BaseResult dayJobExecByHand(JobQryVO jobQryVO) throws BaseException, ParseException {
        List<EtlBase> etlBaseList = null;
        List<DmSqlVO> dmSqlVOList = null;
        JobParametersBuilder parametersBuilder = new JobParametersBuilder();
        etlRunService.runHistoryJob(etlBaseList,dmSqlVOList,jobQryVO,parametersBuilder);
        return new BaseResult(CommonResultConstant.SUCCESS, "执行成功");
    }
}
